<?php
namespace Admin\Controller;
use Think\Controller;

class AreaController extends Controller {

	function __construct() {
		//继承父类
		parent::__construct();

		//判断登录状态
		if(!D('Admin')->islogin()){//未登录
			$this->error('您尚未登录，请先登录！',U('login/login'),3);
		}
		//判断操作是否授权
		if(!D('Admin')->isoperate(CONTROLLER_NAME,ACTION_NAME)){//未授权
			$this->error('您尚无此操作权限！',U('index/welcome'),3);
		}
    }

	//空方法，防止报错
	public function _empty(){
        $this->index();
    }
	
	//省市信息
    public function index(){
		$provincesinfo = M('provinces')->order('id asc')->select();
        $this->assign(provincesinfo,$provincesinfo);
		$this->display();
    }
    //编辑省级信息
    public function editarea(){
        $provinceid= I("get.id");
        if (IS_POST) {
            $info = I("info");
            $flag = M("provinces")->where("id=".$provinceid)->save($info);
            if ($flag) {
                $this->success('省份修改成功！', U('area/index'), 3);
            } else {
                $this->error('省份修改失败！', U('area/index'), 3);
            }
        } else {
            $provinceinfo = M("provinces")->where("id=".$provinceid)->find();
            $this->assign('provinceinfo', $provinceinfo);
            $this->display();
        }
    }
    //添加省级信息
    public function addprovince(){
        if(IS_POST){
            $info = I("info");
            $flag = M("provinces")->add($info);
            if ($flag) {
                $this->success('省份添加成功！', U('area/index'), 3);
            } else {
                $this->error('省份添加失败！', U('area/index'), 3);
            }
        }else{
            $this->display();
        }
    }
    //删除省级信息
    public function delprovince(){
        $provinceid= I("get.id");
        $flag = M("provinces")->where("id=".$provinceid)->delete();
        $flag1 = M("cities")->where("provinceid=".$provinceid)->delete();
        if ($flag && $flag1) {
            $this->success('省份及所含市区删除成功！', U('area/index'), 3);
        } elseif ($flag){
            $this->success('省份删除成功！', U('area/index'), 3);
        } else {
            $this->error('省份删除失败,请重试！', U('area/index'), 3);
        }


    }
    //市级区域信息展示
    public function showcities(){
        $provinceid= I("get.id");
        $citiesinfo = M('cities')->where("provinceid=".$provinceid)->select();
        $this->assign(citiesinfo,$citiesinfo);
        $this->display();
    }

    //添加市级信息
    public function addcity(){
        $provinceid= I("get.id");
        $provinceyoubian=M("provinces")->where("id=".$provinceid)->find();
        $this->assign(provinceyoubian,$provinceyoubian);
        if(IS_POST){
            $info = I("info");
            $info['provinceid']=$provinceid;
            $flag = M("cities")->add($info);
            if ($flag) {
                $this->success('添加市信息成功！', U('area/index'), 3);
            } else {
                $this->error('添加市信息失败！', U('area/index'), 3);
            }
        }else{
            $this->display('');
        }

    }

    //编辑市区信息
    public function editcity(){
        $cityid= I("get.id");
        $result = M('cities')->where("id=".$cityid)->find();
        $id = $result['id'];
        if (IS_POST) {
            $info = I("info");
            $flag = M("cities")->where("id=".$id)->save($info);
            if ($flag) {
                $this->success('市信息修改成功！', U('area/showcities',array('id'=>$result['provinceid'])), 3);
            } else {
                $this->error('市信息修改失败！', U('area/showcities',array('id'=>$result['provinceid'])), 3);
            }
        } else {
            $this->assign(citiesinfo,$result);
            $this->display();
        }
    }
    //删除市级信息
    public function delcity(){
        $cityid= I("get.id");
        $result = M('cities')->where("id=".$cityid)->find();
        $flag = M("cities")->where("id=".$cityid)->delete();
        if ($flag) {
            $this->success('市信息删除成功！', U('area/showcities',array('id'=>$result['provinceid'])), 3);
        } else {
            $this->error('市信息删除失败,请重试！', U('area/showcities',array('id'=>$result['provinceid'])), 3);
        }
    }
}
